var app = getApp();
Page({
  data: {
    listData: [],
    scale: '11',
    controls: '40',
    latitude: '',
    longitude: '',
    markers: [],
    lightList: [],
    pdList: [],
    pdId: '',
    pdLightList: [],
    pdLightIdList: [],
    fakeScale: 11,
    hasSmall: false,
    canLook: false,
    toLat: '',
    toLng: '',
  },
  onLoad: function(options) {
    console.log(options)
    var userName = wx.getStorageSync("userName")
    if (!userName || userName == "" || userName == null) {
      wx.showModal({
        title: '提示',
        content: "用户未登录",
        confirmText: '去登录',
        showCancel: false,
        success: function() {
          wx.redirectTo({
            url: '../login/login',
          })
        }
      })
    } else {
      wx.showLoading({
        title: '加载中',
      })
      this.init(userName, options)
    }
  },
  onReady: function() {
    // 页面渲染完成
    this.mapCtx = wx.createMapContext("map");

  },
  onShow: function() {
    // 页面显示

  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  },
  onShareAppMessage: function() {

  },
  init(userName, options) {
    var that = this
    var params = {
      userName: userName,
      pageNumber: 1,
      pageSize: 1000,
      filter: 1,
      deptId: wx.getStorageSync('deptId'),
      treamId: wx.getStorageSync('treamId'),
      distributionBoxid: options.id,
    }
    var encodingStr = app.ecbEncrypto(params)
    //获取路灯
    wx.request({
      url: app.url + '/web/streetLamp/getList.htm',
      data: encodingStr,
      method: 'POST',
      success: function(res) {
        var decodingStr = app.ecbDecrypto(res.data);
        var rest = JSON.parse(decodingStr)
        console.log(rest)
        if (rest.isSuccess == 0) {
          var list = rest.result.content
          var arr = [];
          that.setData({
            lightList: list
          })
          wx.getLocation({
            type: 'wgs84', //返回可以用于wx.openLocation的经纬度
            success: (res) => {
              that.setData({
                scale: 20,
                longitude: options.longitude,
                latitude: options.latitude,
                markers: that.getLightMarkers(),
              })
            }
          });
        }
      },
      complete: function() {
        wx.stopPullDownRefresh()
        wx.hideLoading()
      }
    })
  },

  getLightMarkers() {
    var market = [];
    for (let item of this.data.lightList) {
      var marker1;
      marker1 = this.createMarker(item)
      market.push(marker1)
    }
    return market;
  },

  createMarker(point) {
    let arr = point.lngLat.split(",");
    let latitude = arr[1];
    let longitude = arr[0];
    let marker = {
      iconPath: point.state == 0 ? "../../images/light_01.png" : "../../images/light_02.png",
      id: point.id || 0,
      state: point.state,
      name: '路灯',
      title: point.address,
      latitude: latitude,
      longitude: longitude,
      exceptionReportId: point.exceptionReportId,
      zIndex: 1,
      label: {
        x: -24,
        y: -26,
        content: ''
      },
      width: 25,
      height: 25
    };
    return marker;
  },
  markertap(e) {
    console.log(e)
    for (var i = 0; i < this.data.markers.length; i++) {
      let item = this.data.markers[i];
      if (item.id == e.markerId && item.name == '路灯') {
        console.log(item)
        this.setData({
          canLook: true,
          toLat: item.latitude,
          toLng: item.longitude,
          toName: item.title
        })
        return
      }
    }

  },
  toLook() {
    wx.openLocation({
      latitude: Number(this.data.toLat),
      longitude: Number(this.data.toLng),
      name: this.data.toName,
      scale: 28
    })
  },
});